#include <QStylePainter>
#include <QStyleOptionButton>
#include <QBrush>
#include <QPainterPath>
#include <QMouseEvent>

#include "CPushButton.h"


CPushButton::CPushButton(QWidget* parent):QPushButton(parent)
{

}

CPushButton::CPushButton(const QString &text, QWidget *parent):QPushButton(text, parent)
{

}

void CPushButton::enterEvent(QEvent *event)
{
    m_isHovered = true;
    update(); // 触发重绘
    QPushButton::enterEvent(event);
}
void CPushButton::leaveEvent(QEvent *event)
{
    m_isHovered = false;
    m_isPressed = false;
    update(); // 触发重绘
    QPushButton::leaveEvent(event);
}
void CPushButton::mousePressEvent(QMouseEvent  *event)
{
    m_isPressed = true;
    update(); // 触发重绘
    QPushButton::mousePressEvent(event);
}
void CPushButton::mouseReleaseEvent(QMouseEvent *event)
{
    m_isPressed = false;
    update(); // 触发重绘
    QPushButton::mouseReleaseEvent(event);
}


void CPushButton::paintEvent(QPaintEvent *event)
{
    if(m_isPressed) {
        paintEventPressed(event);
    } else if(m_isHovered) {
        paintEventHover(event);
    } else {
        paintEventDefault(event);
    }
}


void CPushButton::paintEventDefault(QPaintEvent* event)
{
    Q_UNUSED(event);

    // 先调用基类的 paintEvent 方法，以保留默认的绘制逻辑
    //QPushButton::paintEvent(event);

    // 使用 QStylePainter 来绘制自定义的部分
    QStylePainter painter(this);
    QStyleOptionButton option;
    initStyleOption(&option);

    QRect rect = geometry();

    // 绘制一个圆角矩形作为背景
    QPainterPath path;
    path.addRoundedRect(option.rect, 5, 5);

    // 设置背景颜色
    QColor backgroundColor(Qt::red);
    painter.fillPath(path, QBrush(backgroundColor));

    // 绘制图标
    int nNewIconWidth = 16;
    QIcon icon = this->icon();
    if (!icon.isNull()) {
        int startPosision = (rect.width() - nNewIconWidth) / 2;
        QSize mapSize(nNewIconWidth, nNewIconWidth);
        QPixmap pixmap = icon.pixmap(mapSize);
        painter.drawPixmap(QRect(startPosision, 5, nNewIconWidth, nNewIconWidth), pixmap);
    }

    // 绘制文本
    painter.save();
    QFont font = this->font();
    QFontMetrics metrics(font);
    int lineheight = metrics.ascent();
    int linespacing = 1;

    QStringList text = option.text.split("", QString::KeepEmptyParts);
    option.text.clear();
    QRect newRect(0, nNewIconWidth, geometry().width(), lineheight);
    int rectHeight = text.size() * (lineheight + linespacing) - linespacing;
    for (int i = 0; i < text.size(); ++i) {
        painter.drawText(newRect, Qt::AlignCenter, text[i]);
        newRect.adjust(0, lineheight + linespacing, 0, lineheight + linespacing);
    }
    setFixedHeight(rectHeight + 10);
    painter.restore();
//    Q_UNUSED(event);

//    QStylePainter painter(this);
//    QStyleOptionButton option;
//    initStyleOption(&option);
//    QRect rect = geometry();

//    QPainterPath path;
//    path.addRoundedRect(rect, 5, 5);

//    // 绘制矩形填充整个部件区域
//    painter.fillPath(path, QBrush(QColor(Qt::red))); // Qt::Fill表示填充矩形
//    //painter.fillRect(rect, QBrush(QColor(Qt::red))); // Qt::Fill表示填充矩形
//    //painter.drawRect(this->rect());


//    QIcon icon = this->icon();
//    //QSize iconSize = this->iconSize();
//    QSize rectsizee = size();

//    int nNewIconWidth = 16;//qMin(iconSize.width() + (rectsizee.width() - iconSize.width()) * 0.5, iconSize.height() + (rectsizee.height() - iconSize.height()) * 0.5);
//    //int nNewIconHeidth = nNewIconWidth;
//    int startPosision = (rectsizee.width() - 16) * 0.5;


//    if (icon.isNull()) {
//        // 如果没有设置图标，这里需要添加你的处理逻辑，或者不执行图标绘制部分
//    }
//    else {

//        QSize mapSize(nNewIconWidth, nNewIconWidth);
//        QPixmap pixmap = icon.pixmap(QSize(mapSize));
//        painter.drawPixmap(QRect(startPosision, 5, nNewIconWidth, nNewIconWidth), pixmap);
//    }

//    QStringList text = option.text.split("", QString::KeepEmptyParts);
//    option.text.clear();
//    painter.save();

//    QFont font = this->font();
//    QFontMetrics metrics(font);
//    int lineheight = metrics.ascent();
//    int linespacing = 1;


//    QRect newRect(0, nNewIconWidth, geometry().width(), lineheight);
//    int rectHeight = text.size() * (lineheight + linespacing) - linespacing;
//    for (int i = 0; i < text.size(); ++i)
//    {
//        painter.drawText(newRect, Qt::AlignCenter, text[i]);
//        newRect.adjust(0, lineheight + linespacing, 0,
//                       lineheight + linespacing);
//    }
//    setFixedHeight(rectHeight + 10);
//    painter.restore();
}

void CPushButton::paintEventHover(QPaintEvent* event)
{
    Q_UNUSED(event);

    QStylePainter painter(this);
    QStyleOptionButton option;
    initStyleOption(&option);
    QRect rect = geometry();

    QPainterPath path;
    path.addRoundedRect(option.rect, 5, 5);

    // 绘制矩形填充整个部件区域
    painter.fillPath(path, QBrush(QColor(Qt::green))); // Qt::Fill表示填充矩形
    //painter.fillRect(rect, QBrush(QColor(Qt::red))); // Qt::Fill表示填充矩形
    //painter.drawRect(this->rect());


    QIcon icon = this->icon();
    //QSize iconSize = this->iconSize();
    QSize rectsizee = size();

    int nNewIconWidth = 16;//qMin(iconSize.width() + (rectsizee.width() - iconSize.width()) * 0.5, iconSize.height() + (rectsizee.height() - iconSize.height()) * 0.5);
    //int nNewIconHeidth = nNewIconWidth;
    int startPosision = (rectsizee.width() - 16) * 0.5;


    if (icon.isNull()) {
        // 如果没有设置图标，这里需要添加你的处理逻辑，或者不执行图标绘制部分
    }
    else {

        QSize mapSize(nNewIconWidth, nNewIconWidth);
        QPixmap pixmap = icon.pixmap(QSize(mapSize));
        painter.drawPixmap(QRect(startPosision, 5, nNewIconWidth, nNewIconWidth), pixmap);
    }

    QStringList text = option.text.split("", QString::KeepEmptyParts);
    option.text.clear();
    painter.save();

    QFont font = this->font();
    QFontMetrics metrics(font);
    int lineheight = metrics.ascent();
    int linespacing = 1;


    QRect newRect(0, nNewIconWidth, geometry().width(), lineheight);
    int rectHeight = text.size() * (lineheight + linespacing) - linespacing;
    for (int i = 0; i < text.size(); ++i)
    {
        painter.drawText(newRect, Qt::AlignCenter, text[i]);
        newRect.adjust(0, lineheight + linespacing, 0,
                       lineheight + linespacing);
    }
    setFixedHeight(rectHeight + 10);
    painter.restore();

}

void CPushButton::paintEventPressed(QPaintEvent* event)
{
    Q_UNUSED(event);

    QStylePainter painter(this);
    QStyleOptionButton option;
    initStyleOption(&option);
    QRect rect = geometry();

    QPainterPath path;
    path.addRoundedRect(option.rect, 5, 5);

    // 绘制矩形填充整个部件区域
    painter.fillPath(path, QBrush(QColor(Qt::blue))); // Qt::Fill表示填充矩形
    //painter.fillRect(rect, QBrush(QColor(Qt::red))); // Qt::Fill表示填充矩形
    //painter.drawRect(this->rect());


    QIcon icon = this->icon();
    //QSize iconSize = this->iconSize();
    QSize rectsizee = size();

    int nNewIconWidth = 16;//qMin(iconSize.width() + (rectsizee.width() - iconSize.width()) * 0.5, iconSize.height() + (rectsizee.height() - iconSize.height()) * 0.5);
    //int nNewIconHeidth = nNewIconWidth;
    int startPosision = (rectsizee.width() - 16) * 0.5;


    if (icon.isNull()) {
        // 如果没有设置图标，这里需要添加你的处理逻辑，或者不执行图标绘制部分
    }
    else {

        QSize mapSize(nNewIconWidth, nNewIconWidth);
        QPixmap pixmap = icon.pixmap(QSize(mapSize));
        painter.drawPixmap(QRect(startPosision, 5, nNewIconWidth, nNewIconWidth), pixmap);
    }

    QStringList text = option.text.split("", QString::KeepEmptyParts);
    option.text.clear();
    painter.save();

    QFont font = this->font();
    QFontMetrics metrics(font);
    int lineheight = metrics.ascent();
    int linespacing = 1;


    QRect newRect(0, nNewIconWidth, geometry().width(), lineheight);
    int rectHeight = text.size() * (lineheight + linespacing) - linespacing;
    for (int i = 0; i < text.size(); ++i)
    {
        painter.drawText(newRect, Qt::AlignCenter, text[i]);
        newRect.adjust(0, lineheight + linespacing, 0,
                       lineheight + linespacing);
    }
    setFixedHeight(rectHeight + 10);
    painter.restore();
}
